package com.xsm.ppsecret.ui.pwd;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xsm.ppsecret.R;
import com.xsm.ppsecret.presenter.AddPwdPresenter;
import com.xsm.ppsecret.ui.BaseActivity;
import com.xsm.ppsecret.ui.implview.IAddPwdActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AddPwdActivity extends BaseActivity implements IAddPwdActivity, View.OnClickListener {

    @BindView(R.id.menu_normal_title)
    TextView menuTitle;
    @BindView(R.id.menu_normal_action)
    TextView menuAction;
    @BindView(R.id.menu_normal_back)
    ImageView menuBack;
    @BindView(R.id.edit_title)
    EditText title;
    @BindView(R.id.edit_username)
    EditText username;
    @BindView(R.id.edit_password)
    EditText password;
    @BindView(R.id.edit_remarks)
    EditText remarks;
    private AddPwdPresenter mPresenter = new AddPwdPresenter(this, this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_pwd);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        menuTitle.setText("添加密码");
        menuAction.setText("保存");
        menuBack.setOnClickListener(this);
        menuAction.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu_normal_back:
                finish();
                break;
            case R.id.menu_normal_action:
                mPresenter.save(this, title.getText().toString(), username.getText().toString(), password.getText().toString(), remarks.getText().toString());
                break;
        }
    }

    @Override
    public void saveError(String errorMsg) {
        Toast.makeText(this, errorMsg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void saveOk() {
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_OK);
        super.onBackPressed();
        finish();
    }
}
